//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System.Windows.Media;

    /// <summary>
    /// This class is used to show the Input Control to the user.
    /// This would be Shown to the user when he Double Cliks on the Parameter Element
    /// and the user will be promted to enter data.
    /// </summary>
    public class InputBox : Window
    {
        /// <summary>
        /// Cache for the InputBox instance.
        /// </summary>
        private static InputBox instance;

        /// <summary>
        /// Cache for the result of the action.
        /// </summary>
        private InputBoxResult result = new InputBoxResult(MessageBoxResult.None, String.Empty);

        /// <summary>
        /// The control to get data from the user.
        /// </summary>
        private UserDataEntryBaseCtrl usrDataEntryCtrl;

        /// <summary>
        /// Flag specifying if null values can be allowed or not.
        /// </summary>
        private bool allowNullValue;

        /// <summary>
        /// Gets and sets the flag indicating whether null will be allowed as value or not.
        /// </summary>
        public bool AllowNullValue
        {
            get
            {
                return this.allowNullValue;
            }
            set
            {
                this.allowNullValue = value;
                if (this.usrDataEntryCtrl != null)
                {
                    this.usrDataEntryCtrl.AllowNullAsValue = this.allowNullValue;
                }
            }
        }

        /// <summary>
        /// Contructor of the Class.
        /// </summary>
        private InputBox()
        {
            this.InitComponent();
        }

        /// <summary>
        /// Show the user the dialog to enter the Data.
        /// This dialog entry box will not be updated with default data.
        /// </summary>
        /// <param name="caption">Caption for the Dialog.</param>
        /// <param name="message">Parameter Label.</param>
        /// <param name="initialValue">Default value.</param>
        /// <returns>Whether Ok or Cancel.</returns>
        public static InputBoxResult Show(string caption, string message, string initialValue, bool allowNullValue)
        {
            if (instance == null)
            {
                instance = new InputBox();
            }

            instance.usrDataEntryCtrl.MainHeader = caption;
            instance.usrDataEntryCtrl.UserPromptText = message;
            instance.usrDataEntryCtrl.CurrentInfo = initialValue;
            instance.AllowNullValue = allowNullValue;
            instance.ShowDialog();
            return instance.result;
        }

        /// <summary>
        /// Sets the cancel of teh dialog.
        /// </summary>
        /// <param name="ev">Cancel Event Args.</param>
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            base.OnClosing(e);
        }

        /// <summary>
        /// Creation of teh Dialog.
        /// </summary>
        private void InitComponent()
        {            
            this.Width = 200;         
            this.Height = 120;
            this.ResizeMode = ResizeMode.NoResize;
            this.ShowInTaskbar = false;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.WindowStyle = WindowStyle.None;
            this.BorderBrush = new SolidColorBrush(Colors.Transparent);
            this.BorderThickness = new Thickness(0, 0, 0, 0);
            this.Owner = Application.Current.MainWindow;

            this.usrDataEntryCtrl = new UserDataEntryBaseCtrl(this.allowNullValue);
            this.usrDataEntryCtrl.InfoEntered += new EventHandler<InfoEnteredEventArgs>(this.OnInfoEntered);
            this.usrDataEntryCtrl.HeaderDrag += (sender, e) => { if (this.IsVisible) { this.DragMove(); } };
            this.Content = usrDataEntryCtrl;
        }

        /// <summary>
        /// Fired on user entering the information for the input parameters.
        /// </summary>
        /// <param name="sender">
        /// This.
        /// </param>
        /// <param name="e">
        /// InfoEnteredEventArgs.
        /// </param>
        private void OnInfoEntered(object sender, InfoEnteredEventArgs e)
        {
            if (e.IsCancel)
            {
                this.result.ButtonResult = MessageBoxResult.Cancel;
            }
            else
            {
                this.result.ButtonResult = MessageBoxResult.OK;
                this.result.UserInput = e.InfoEntered;
            }

            this.Hide();
        }
    }
}
